﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WMPLib;

namespace MusicManager
{
    public partial class MainForm : Form
    {
        private List<MusicListForm> listForms = new List<MusicListForm>();
        private Dictionary<string, IWMPMedia> dicSongs = new Dictionary<string, IWMPMedia>();// (path, item)
        private static string[] defaultNames = { "don", "激情", "柔和", "壮阔", "欢快", "紧张", "悬疑", "待定" };
        private string tmpListName = "";
        private int listFormCount = 0;
        private int curListIndex = -1;

        public MainForm()
        {
            InitializeComponent();
            player.currentPlaylist = player.playlistCollection.newPlaylist(defaultNames[0]);
            InitPlayerList(null);
        }

        public void InitPlayerList(string[] paths)
        {
            AddToCurPlayList(paths);
            player.Ctlcontrols.play();
        }

        public void AddToCurPlayList(string[] paths)
        {
            if (paths != null)
            {
                foreach (string music in paths)
                {
                    IWMPMedia item = player.newMedia(music);
                    player.currentPlaylist.appendItem(item);
                    dicSongs.Add(music, item);
                }
            }
        }

        public void ClearPlayList()
        {
            player.currentPlaylist.clear();
            dicSongs.Clear();
        }

        /// <summary>
        /// return true if playlist changed, false if not
        /// </summary>
        /// <param name="listFormIndex"></param>
        /// <param name="paths"></param>
        /// <returns></returns>
        public bool ChangePlayList(int listFormIndex, string[] paths)
        {
            if (curListIndex != listFormIndex)
            {
                curListIndex = listFormIndex;
                ClearPlayList();
                AddToCurPlayList(paths);
                return true;
            }
            return false;
        }

        public void AddToPlayList(int listFormIndex, string[] paths)
        {
            if (curListIndex == listFormIndex)
            {
                AddToCurPlayList(paths);
            }
        }

        public MusicListForm AddListForm(string listName, IWMPPlaylist playList, int index)
        {
            MusicListForm listForm = new MusicListForm(this, listName,index);
            listForm.InitList(playList);
            listForms.Add(listForm);
            return listForm;
        }

        private void listButton_Click(object sender, EventArgs e)
        {
            if (listForms.Count == 0)
            {
                foreach(string name in defaultNames)
                    AddListForm(name, null, listFormCount++);
            }
            foreach (MusicListForm listForm in listForms)
            {
                //if (listForm == null || listForm.IsDisposed)
                //{
                //    listForm = new MusicListForm(this);
                //    listForm.InitList(player.currentPlaylist);
                //}
                listForm.Show();
            }
        }

        public void DeleteCurListItem(string path, IWMPMedia song)
        {
            dicSongs.Remove(path);
            player.currentPlaylist.removeItem(song);
        }

        public void DeleteItem(int index, string path)
        {
            if (index == curListIndex)
            {
                if (dicSongs.ContainsKey(path))
                    DeleteCurListItem(path, dicSongs[path]);
            }
        }

        public IWMPPlaylist GetCurList()
        {
            return player.currentPlaylist;
        }

        public AxWMPLib.AxWindowsMediaPlayer GetPlayer()
        {
            return player;
        }

        public IWMPMedia GetMedia(string path)
        {
            if (dicSongs.ContainsKey(path))
                return dicSongs[path];
            else
                return null;
        }

        private void buttonAddListForm_Click(object sender, EventArgs e)
        {
            ListInfoForm infoForm = new ListInfoForm();
            infoForm.ClosingEvent+=new ListInfoForm.EventDelegate(GetListName);
            if (infoForm.ShowDialog() == DialogResult.OK)
            {
                //MusicListForm newForm = new MusicListForm(this, tmpListName,listFormCount++);
                //listForms.Add(newForm);
                AddListForm(tmpListName, null, listFormCount++).Show();
            }
        }

        public void GetListName(string listName)
        {
            tmpListName = listName == "" ? defaultNames[0] : listName;
        }
    }
}
